package com.mall.live.controller;

import com.github.pagehelper.PageInfo;
import com.mall.common.annotation.IngoreResponseAdvice;
import com.mall.common.response.CommonResponse;
import com.mall.entity.TbFreight;
import com.mall.entity.live.TbLive;
import com.mall.entity.live.TbLiveFollower;
import com.mall.live.service.ILiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author  LiJinYang
 * @email  283006176@qq.com
 * @date 2021/07/27 18:51
 * @version 1.0
 */
@RestController
@RequestMapping("/live")
@CrossOrigin
public class LiveController {

    @Autowired
    private ILiveService liveService;

    /**
     * 获取直播次数分析图表数据
     * @param id
     * @return
     */
    @GetMapping("/findAll")
    @IngoreResponseAdvice
    public Map<String, List<TbLive>> findAll(Integer id){
        return liveService.findAll(id);
    }

    /**
     * 获取订单反馈分析图表数据
     * @param id
     * @return
     */
    @GetMapping("/findFeedBack")
    public Map<String,List<TbLive>> findFeedBack(Integer id){
        return liveService.findFeedBack(id);
    }

    /**
     * 创建直播
     * @param live
     * @param itmes
     * @return
     */
    @PostMapping("/addLive")
    public CommonResponse addLive(@RequestBody TbLive live,@RequestParam("itmes") Long[] itmes,@RequestParam("token") String token){
        return liveService.addLive(live,itmes,token);
    }

    /**
     * 直播列表（模糊）
     * @param tbLive
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/findPage")
    public PageInfo findPage(@RequestBody TbLive tbLive
            , @RequestParam(value = "page",defaultValue = "1")int page
            ,@RequestParam(value = "size",defaultValue = "20")int size){
        return liveService.findPage(tbLive,page,size);
    }

    /**
     * 修改直播状态
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/update")
    public CommonResponse update(@RequestParam("id")Long id,@RequestParam("status")String status){
        try{
            liveService.update(id,status);
            return new CommonResponse(0,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(-1,"修改失败");
        }
    }

    /**
     * 执行逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public CommonResponse delete(Long id){
        try{
            liveService.delete(id);
            return new CommonResponse(0,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(-1,"删除失败");
        }
    }

    /**
     * 直播结束 修改直播状态
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/updateStatusById")
    public CommonResponse updateStatusById(Integer id,String status){
        return liveService.updateStatusById(id,status);
    }

    /**
     * 查询当前直播信息
     * @param id
     * @return
     */
    @GetMapping("/findOne")
    public TbLive findOne(Integer id){
        return liveService.findOne(id);
    }

    /**
     * 显示直播人、在线人数、
     * @param id
     * @return
     */
    @GetMapping
    public CommonResponse liveDetails(Long id){
        return liveService.liveDetails(id);
    }

    /**
     * 进入直播间时修改观看数量
     * @param id
     */
    @PutMapping("/liveInto/{id}")
    public void liveInto(@PathVariable Long id){
        liveService.liveInto(id);
    }

    @GetMapping("/findLiveList")
    public List<TbLive> findLiveList(){
        return liveService.findLiveList();
    }

    @GetMapping("/findLiveRecommend")
    public List<TbLive> findLiveRecommend(){
        return liveService.findLiveRecommend();
    }

    /**
     * 查询当前直播间观看人（只显示十个)
     * @param lid
     * @return
     */
    @GetMapping("/watch")
    public List<TbLiveFollower> watch(Long lid){
        return liveService.watch(lid);
    }


    /**
     * 我的直播间
     * @param token
     * @return
     */
    @GetMapping("/myLive")
    public List<TbLive> myLive(String token){
        return liveService.myLive(token);
    }

    /**
     * 开启直播
     * @param id
     * @return
     */
    @GetMapping("/start")
    public CommonResponse start(Integer id){
        try{
            return new CommonResponse(0,"开启直播",liveService.start(id));
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(-1,"直播开启失败");
        }
    }

    /**
     * 更新在线人数
     * @param id
     * @return
     */
    @GetMapping("/liveNum")
    public CommonResponse liveNum(Integer id){
        try{

            return new CommonResponse(0,"进入直播",liveService.liveNum(id));
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResponse(-1,"进入直播失败");
        }
    }
}
